package com.erpanderp.wam.ac.model.service.reimbursement;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.ProjectPerdiem;
import com.erpanderp.wam.ac.model.entity.reimbursement.ProjectPerdiemDetail;
import com.erpanderp.wam.util.HibernateUtil;

public class ProjectPerdiemDAO {
	
	public ProjectPerdiem getProjectPerdiemByProjectAssignmentId(Long projectAssignmentId){
		ProjectPerdiem projectPerdiem = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from ProjectPerdiem as p where p.projectAssignment.projectAssignmentId =:projectAssignmentId";
			Query query = session.createQuery(queryString).setLong("projectAssignmentId", projectAssignmentId);
			projectPerdiem = (ProjectPerdiem) query.uniqueResult();
			return projectPerdiem;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
			
		
		
	}
	public String getLastGeneratedPerdiemRivision(){
	
	String queryString="select p.revision from ProjectPerdiemDetail p ";
	
	System.out.println("QUERY: " + queryString);
	
	List<String> codeList = null;
	
	try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();		
		Query query = session.createQuery(queryString);
		query.setCacheable(false);
		codeList = (List<String>) query.list();
		session.getTransaction().commit();
	}catch(HibernateException e){
		e.printStackTrace();
	}
	
	String lastCode = null;
	for (String code : codeList) {
		
		if (lastCode == null) {
			lastCode = code;
			continue;
			
		}else if (Long.parseLong(code.substring(1)) > Long.parseLong(lastCode.substring(1))) {
				lastCode = code;
				continue;
			
		}
		System.out.println(lastCode + "============ " + code);
	}		
	return lastCode;	
	}
	
	public boolean createProjectPerdiem(ProjectPerdiem projectPerdiem){
		System.out.println("IN CREATE PROJECTPERDIEM ");
		System.out.println("PROJECTPERDIEM  TOTAL AMOUNT ");
		boolean saved = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
		    session.save(projectPerdiem);
		    
		    session.getTransaction().commit();
		    saved = true;
		    return saved;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
			// TODO: handle exception
		}
		
	}
	
	public ProjectPerdiem getProjectPerdiemByProjectPerdiemCode(String projectPerdiemCode){
		System.out.println("GET PROJECT PERDIEM BY PROJECT PERDIEM CODE");
		ProjectPerdiem projectPerdiem = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from ProjectPerdiem p where p.projectPerdiemCode=:projectPerdiemCode";
			Query query = session.createQuery(queryString).setString("projectPerdiemCode", projectPerdiemCode);
			projectPerdiem = (ProjectPerdiem) query.uniqueResult();
			return projectPerdiem;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	public ProjectPerdiem getProjectPerdiemByPerdiemCodeAndEmployeeId(String projectPerdiemCode,Long employeeId){
		System.out.println("GET PROJECT PERDIEM BY PROJECT PERDIEM CODE AND EMPLOYEE ID");
		ProjectPerdiem projectPerdiem = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from ProjectPerdiem p where p.projectPerdiemCode=:projectPerdiemCode and p.employee.employeeId=:employeeId";
			Query query = session.createQuery(queryString).setString("projectPerdiemCode", projectPerdiemCode).setLong("employeeId", employeeId);
			projectPerdiem = (ProjectPerdiem) query.uniqueResult();
			return projectPerdiem;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	
	public List<ProjectPerdiem> getProjectPerdiemsByWamUserId(Long employeeId){
		System.out.println("GET PROJECT PERDIEM BY PROJECT PERDIEM CODE");
		List<ProjectPerdiem> projectPerdiemsList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from ProjectPerdiem p where p.employee.employeeId=:employeeId";
			//String queryString2 = "from projectperdiemdetail where REVISION in(select max(REVISION) AS REVISION from projectperdiemdetail group by(PROJECTPERDIEMID));";
			Query query = session.createQuery(queryString).setLong("employeeId", employeeId);
			projectPerdiemsList = (List<ProjectPerdiem>) query.list();
			return projectPerdiemsList;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	
	public ProjectPerdiemDetail getProjectPerdiemDetailsByProjectPerdiemId(Long projectPerdiemDetailId){
		ProjectPerdiemDetail projectPerdiemDetails = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from ProjectPerdiemDetail p where p.projectPerdiemDetailId=:projectPerdiemDetailId";
			Query query = session.createQuery(queryString).setLong("projectPerdiemDetailId", projectPerdiemDetailId);
			projectPerdiemDetails = (ProjectPerdiemDetail) query.uniqueResult();
			session.getTransaction().commit();
			return projectPerdiemDetails;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	
	public ProjectPerdiemDetail getProjectPerdiemByProjectPerdiemDetailId(Long projectPerdiemDetailId){
		ProjectPerdiemDetail projectPerdiemDetail = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from ProjectPerdiemDetail p where p.projectPerdiemDetailId=:projectPerdiemDetailId";
			Query query = session.createQuery(queryString).setLong("projectPerdiemDetailId", projectPerdiemDetailId);
			projectPerdiemDetail = (ProjectPerdiemDetail) query.uniqueResult();
			session.getTransaction().commit();
			return projectPerdiemDetail;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
/*	public boolean updateRecommendProjectPerdiemDetails(ProjectPerdiemDetail projectPerdiemDetail){
		boolean updated = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
		    session.saveOrUpdate(projectPerdiemDetail);
			session.getTransaction().commit();
			updated = true;
			return updated;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
			// TODO: handle exception
		}
		
	}
	*/
	 public boolean updateProjectPerdiemsDetails(ProjectPerdiemDetail projectPerdiemDetail) {
		 boolean updated = false;
    	 try{
    		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    		 session.setFlushMode(FlushMode.COMMIT);
    		 session.beginTransaction();
    		 session.saveOrUpdate(projectPerdiemDetail);
    		 session.getTransaction().commit();
    		 updated = true;
    		 return updated;
    	 }catch(Exception e){
    		 e.printStackTrace();
    		 return false;
    	 }
    	
 }
	 
	 
	/* public boolean updateProjectPerdiems(ProjectPerdiemDetail projectPerdiemDetail) {
		 boolean updated = false;
    	 try{
    		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    		 session.setFlushMode(FlushMode.COMMIT);
    		 session.beginTransaction();
    		 session.save(projectPerdiemDetail);
    		 session.getTransaction().commit();
    		 updated = true;
    		 return updated;
    	 }catch(Exception e){
    		 e.printStackTrace();
    		 return false;
    	 }
    	
 }
	*/
	 
	 public String getRevisionCountByPerdiemId(Long projectPerdiemId) {
		 Long revisionCount = null;
    	 try{
    		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    		 session.setFlushMode(FlushMode.AUTO);
    		 session.beginTransaction();
    		 String queryString = "select Count(*) from ProjectPerdiemDetail as p where p.projectPerdiem.projectPerdiemId=:projectPerdiemId";
    		 Query query = session.createQuery(queryString);
    	     query.setCacheable(false).setLong("projectPerdiemId",projectPerdiemId);;
    	     revisionCount = (Long) query.uniqueResult();
    	     session.getTransaction().commit();
    		 
    	 }catch(Exception e){
    		 e.printStackTrace();
    		
    	 }
    	 return revisionCount.toString();
 }
	 public static void main(String args[]){
		 //System.out.println("COUNT"+new ProjectPerdiemDAO().getRevisionCountByPerdiemCode(1L));
	 }
	
	

}
